package io2;

import java.io.*;
import java.util.Scanner;

public class File1 {

    public static void main(String[] args) {
        try (InputStream is = new FileInputStream("testDir/a.txt")) {
            try (Scanner scanner = new Scanner(is, "UTF-8")) {
                while (scanner.hasNext()) {
                    String s = scanner.next();
                    System.out.print(s);
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main3(String[] args) {
        try (InputStream is = new FileInputStream("testDir/a.txt")) {
            byte[] bytes = new byte[1024];
            int len = 0;
            while (true) {
                len = is.read(bytes);
                if (len == -1) {

                    System.out.println();
                    System.out.println("文件已读完");
                    break;
                }
                for (int i = 0; i < len; i+=3) {
                    String s = new String(bytes, i, 3, "UTF-8");
                    System.out.printf("%s", s);
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main2(String[] args) {
        try (InputStream is = new FileInputStream("testDir/a.txt")) {
            byte[] bytes = new byte[1024];
            int len = 0;
            while (true) {
                len = is.read(bytes);
                if (len == -1) {

                    System.out.println();
                    System.out.println("文件已读完");
                    break;
                }
                for (int i = 0; i < len; i++) {
                    System.out.printf("%c",bytes[i]);
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main1(String[] args) throws FileNotFoundException {
        try (InputStream inputStream = new FileInputStream("testDir/a.txt")) {
            while (true) {
                int i = inputStream.read();
                if (i == -1) {
                    System.out.println();
                    System.out.println("File reading finished");
                    break;
                }
                System.out.printf("%c",i);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main0(String[] args) throws FileNotFoundException {

        File file = new File("./testDir/a.txt");

        OutputStream outputStream = new FileOutputStream("./testDir/a.txt");


    }

}